package cn.frontop.funswitch.service.impl;

import cn.frontop.funswitch.dao.TokenDao;
import cn.frontop.funswitch.entity.ProjectEntity;
import cn.frontop.funswitch.entity.RequestDto;
import cn.frontop.funswitch.entity.TokenEntity;
import cn.frontop.funswitch.enums.RequestEnum;
import cn.frontop.funswitch.service.ProjectService;
import cn.frontop.funswitch.service.TokenService;
import cn.frontop.funswitch.util.CommonUtil;
import cn.hutool.cache.Cache;
import cn.hutool.cache.CacheUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jayway.jsonpath.JsonPath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author Yun
 */
@Service("tokenService")
public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> implements TokenService {

    @Autowired
    ProjectService projectService;
    /**
     * 缓存Token
     */
    private final Cache<Integer, String> CACHE = CacheUtil.newFIFOCache(100);

    @Override
    public String getTokenById(Integer tokenId) {
        TokenEntity tokenEntity = this.getById(tokenId);
        CommonUtil.isEmpty(tokenEntity, "token不存在!!!");
        ProjectEntity project = projectService.getById(tokenEntity.getProjectId());
        CommonUtil.isEmpty(project, "项目不存在!!!");
        //判断是否有缓存设置
        boolean isCache = !(tokenEntity.getTokenCache() == null || tokenEntity.getTokenCache().equals(0));
        if (isCache && CACHE.get(tokenId) != null) {
            return CACHE.get(tokenId);
        }
        //拼接公用 请求
        tokenEntity.setUrl(project.getDomian() + tokenEntity.getUrl());
        //工厂获取结果
        RequestEnum requestEnum = RequestEnum.valueOf(tokenEntity.getRequestType().toUpperCase());
        //转换需要的参数
        RequestDto requestDto = new RequestDto();
        requestDto.setUrl(tokenEntity.getUrl());
        requestDto.setBody(tokenEntity.getBody());
        //源一定要为上级!
        requestDto.setHeaders(CommonUtil.apply(project.getHeader(), tokenEntity.getHeaders()));
        //请求结果
        String conversion = requestEnum.conversion(requestDto);
        //返回需要的
        String token = JsonPath.read(conversion, tokenEntity.getJsonPath()).toString();
        String prefixToken = tokenEntity.getPrefix() + (JSONUtil.isTypeJSONArray(token) ? JSONUtil.parseArray(token).getStr(0) : token);
        if (isCache) {
            //永久
            if (tokenEntity.getTokenCache().equals(1)) {
                CACHE.put(tokenId, prefixToken);
            } else {
                CACHE.put(tokenId, prefixToken, tokenEntity.getTokenCache() * 60 * 1000);
            }
        }
        return prefixToken;
    }

    @Override
    public Object getTokenByEntity(TokenEntity tokenEntity) {
        CommonUtil.isEmpty(tokenEntity, "token不存在!!!");
        ProjectEntity project = projectService.getById(tokenEntity.getProjectId());
        CommonUtil.isEmpty(project, "项目不存在!!!");
        //拼接公用 请求
        tokenEntity.setUrl(project.getDomian() + tokenEntity.getUrl());
        //工厂获取结果
        RequestEnum requestEnum = RequestEnum.valueOf(tokenEntity.getRequestType().toUpperCase());
        //转换需要的参数
        RequestDto requestDto = new RequestDto();
        requestDto.setUrl(tokenEntity.getUrl());
        requestDto.setBody(tokenEntity.getBody());
        //源一定要为上级!
        requestDto.setHeaders(CommonUtil.apply(project.getHeader(), tokenEntity.getHeaders()));
        //请求结果
        String conversion = requestEnum.conversion(requestDto);
        //返回需要的
        String result = StrUtil.isEmpty(tokenEntity.getJsonPath()) ? conversion : tokenEntity.getPrefix() + (JSONUtil.isTypeJSONArray(JsonPath.read(conversion, tokenEntity.getJsonPath()).toString()) ? JSONUtil.parseArray(JsonPath.read(conversion, tokenEntity.getJsonPath()).toString()).getStr(0) : JsonPath.read(conversion, tokenEntity.getJsonPath()).toString());
        return CommonUtil.isJson(result);
    }

    @Override
    public void removeCache(Integer tokenId) {
        CACHE.remove(tokenId);
    }
}